// 创建 XHR 对象
// 原方案
function createXHR1() {
	// 标准浏览器
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	// IE 浏览器
	} else if (typeof ActiveXObject != 'undefined') {
		if (typeof arguments.callee.activeXString != 'string') {
			var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
				i = 0,
				len = versions.length;
			// 遍历并设置版本
			for (; i < len; i++) {
				try {
					new ActiveXObject(versions[i]);
					arguments.callee.activeXString = versions[i];
					break;
				} catch(ex) {
				}
			}
		}
		return new ActiveXObject(arguments.callee.activeXString);
	// 对不支持的浏览器抛出错误提示
	} else {
		throw new Error('您的浏览器并不支持Ajax');
	}
}


// 改进：方案一：加载时损失性能，但是第一次调用时不损失性能
var createXHR2 = function() {
	if (typeof XMLHttpRequest != 'undefined') {
		return function() {
			return new XMLHttpRequest();
		}
	} else if (typeof ActiveXObject != 'undefined') {
		return function() {
			if (typeof arguments.callee.activeXString != 'string') {
				var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
					i = 0,
					len = versions.length;
				// 遍历并设置版本
				for (; i < len; i++) {
					try {
						new ActiveXObject(versions[i]);
						arguments.callee.activeXString = versions[i];
						break;
					} catch(ex) {
					}
				}
			}
			return new ActiveXObject(arguments.callee.activeXString);
		}
	} else {
		return function () {
			throw new Error('No XHR object available.');
		}
	}
}();


// 改进：方案二：加载时不损失性能，第一次调用时损失性能
var createXHR3 = function() {
	if (typeof XMLHttpRequest != 'undefined') {
		createXHR3 = function() {
			return new XMLHttpRequest();
		}
	} else (typeof ActiveXObject != 'undefined') {
		createXHR3 = function() {
			if (typeof arguments.callee.activeXString != 'string') {
				var versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
					i = 0,
					len = versions.length;
				// 遍历并设置版本
				for (; i < len; i++) {
					try {
						new ActiveXObject(versions[i]);
						arguments.callee.activeXString = versions[i];
						break;
					} catch(ex) {
					}
				}
			}
			return new ActiveXObject(arguments.callee.activeXString);
		}
	} else {
		createXHR3 = function() {
			throw new Error('NO XHR object available.');
		}
	}
}

